package com.x.client.fragment

import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentStatePagerAdapter
import com.alibaba.android.arouter.launcher.ARouter
import com.gg.baselibrary.BaseFragment
import com.gg.image.config.GlideApp
import com.jakewharton.rxbinding2.view.RxView
import com.x.client.R
import com.x.client.utils.RouterClass
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_home.view.*
import java.util.concurrent.TimeUnit

/**
 * Creator : GG
 * Date    : 2018/5/21
 * Mail    : gg.jin.yu@gmai.com
 * Explain :
 */
class HomeFragment : BaseFragment() {

    companion object {
        fun newInstance(): HomeFragment = HomeFragment()
    }

    override fun getLayoutResId(): Int = R.layout.fragment_home


    private val titles: List<String> by lazy { listOf("启辕认证", "订阅车源", "最新车源") }

    override fun initView() {
        super.initView()
        initBanner()
        tabLayout.apply {
            addTab(tabLayout.newTab().apply {
                text = "启辕认证"
            })
            addTab(tabLayout.newTab().apply {
                text = "订阅车源"
            })
            addTab(tabLayout.newTab().apply {
                text = "最新车源"
            })
        }

        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                viewPager.currentItem = tab?.position ?: 0
            }
        })
        val fragments = arrayListOf<Fragment>()

        fragments.add(CarListFragment.newInstance())
        fragments.add(CarListFragment.newInstance())
        fragments.add(CarListFragment.newInstance())



        viewPager.adapter = object : FragmentStatePagerAdapter(childFragmentManager) {
            override fun getItem(position: Int): Fragment = fragments[position]

            override fun getCount(): Int = fragments.size

            override fun getPageTitle(position: Int): CharSequence? = titles[position]

        }
        tabLayout.setupWithViewPager(viewPager)



        RxView.clicks(brandMore)
                .throttleFirst(1, TimeUnit.SECONDS)
                .compose(this.bindToLifecycle())
                .subscribe {
                    ARouter.getInstance().build(RouterClass.brand).navigation()
                }

    }

    private fun initBanner() {
//        banner.apply {
//            setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
//            setImageLoader(RotationImageLoader())
//            setBannerAnimation(Transformer.Default)
//            isAutoPlay(true)
//            setDelayTime(1500)
//            setIndicatorGravity(BannerConfig.CENTER)
//        }
//        banner.setImages(listOf("http://seopic.699pic.com/photo/40007/4482.jpg_wh1200.jpg"))
//        banner.startAutoPlay()
        GlideApp.with(this).load("http://seopic.699pic.com/photo/40007/4482.jpg_wh1200.jpg").into(titleImg)
    }

}